<?php
namespace app\admin\controller;

use controller\BasicAdmin;
use service\DataService;
use think\Db;

/**
 * 子公司管理控制器
 * Class Subsidiary
 * @package app\admin\controller
 * @author ma <80234043@qq.com>
 * @date 2022/02/12 18:12
 */
class Subsidiary extends BasicAdmin
{
    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'SystemSubsidiary';

    //列表
    public function index(){
        $this->title = '子公司公司管理';
        $get = $this->request->get();
        $db = Db::name($this->table)->where(['is_deleted' => '0']);
        foreach (['subsidiary', 'country'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike($key, "%{$get[$key]}%");
        }
        return parent::_list($db);

    }

    //添加
    public function add(){
        return $this->_form($this->table, 'form',$pkField = '', $where = [], $extendData = ['create_at'=>time(),'create_by'=>session("user.id")]);
    }

    //修改
    public function edit(){
        return $this->_form($this->table, 'form',$pkField = '', $where = [], $extendData = ['create_at'=>time(),'create_by'=>session("user.id")]);
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_data_filter(&$data)
    {
        if(count($data)>0){
            //集团公司id
            $group_ids = array_column($data, 'group_company_id');
            //批量获取集团公司名称
            $group_company_names = Db::name('SystemGroupCompany')->where(['id'=>['IN',$group_ids]])->column('name','id');
            $countrys = config('country_map');
            foreach ($data as &$vo) {
                $vo['groupcompany'] = isset($group_company_names[$vo['group_company_id']]) ? $group_company_names[$vo['group_company_id']] :'';
                $vo['country'] = isset($countrys[$vo['country']]) ? $countrys[$vo['country']]: '';
            }

        }
    }

    /**
     * 表单数据前缀方法
     * @param array $vo
     */
    protected function _form_filter(&$vo)
    {
        if ($this->request->isGet()) {
            //获取集团公司信息
            $groups = Db::name('SystemGroupCompany')->where(['is_deleted'=>0])->order('id desc')->select();
            $this->assign('groups', $groups);
            
        }
    }


    //删除
    public function del(){
        if (DataService::update($this->table)) {
            $this->success("删除成功!", '');
        }
        $this->error("删除失败, 请稍候再试!");
    }
}